package com.service.impl;

import com.bean.Class;
import com.bean.Teacher;
import com.dao.TeacherDao;
import com.dao.impl.TeacherDaoImpl;
import com.service.TeacherService;
import com.utils.Page;
import com.utils.WebUtils;

import java.util.List;

public class TeacherServiceImpl implements TeacherService {
    TeacherDao teacherDao = new TeacherDaoImpl();

    @Override
    public int login(Teacher record) {
        int status = existsUsername(record.getNo());
        if (status == 0) {
            Teacher teacher = teacherDao.selectBy(record);
            if (teacher != null) {
                WebUtils.copyBean(teacher, record);
            } else {
                status = 2;
            }
        }
        return status;
    }

    @Override
    public int existsUsername(String username) {
        int status = 1;
        Teacher teacher = new Teacher();
        teacher.setNo(username);
        teacher = teacherDao.selectBy(teacher);
        if (teacher != null) {
            if (teacher.getNo().equals(username)) {
                status = 0;
            }
        }
        return status;
    }

    @Override
    public int updatePassword(Integer id, String password, String newPassword) {
        int status = 0;
        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setPassword(password);
        teacher = teacherDao.selectBy(teacher);
        if (teacher != null) {
            teacher.setPassword(newPassword);
            teacherDao.updateByPrimaryKey(teacher);
        } else {
            status = 1;
        }
        return status;
    }

    @Override
    public int updeteInfo(Teacher record) {
        int status = 0;
        if (teacherDao.updateByPrimaryKey(record) == -1) {
            status = 1;
        }
        return status;
    }

    @Override
    public int insert(Teacher record) {
        int status = 0;
        Teacher teacher = new Teacher();
        teacher.setNo(record.getNo());
        teacher = teacherDao.selectBy(teacher);
        if (teacher != null) {
            status = 1;
        } else if (teacherDao.insert(record) == -1) {
            status = 1;
        }
        return status;
    }

    @Override
    public Page<Teacher> page(int pageNo, int pageSize, String content) {
        Page<Teacher> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = teacherDao.selectForPageTotalCount(content);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Teacher> items = teacherDao.selectForPageItems(begin, pageSize, content);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public Teacher selectBy(Teacher teacher) {
        return teacherDao.selectBy(teacher);
    }

    @Override
    public int deleteById(Integer id) {
        int status = 0;
        if (teacherDao.deleteByPrimaryKey(id) == -1) {
            status = 1;
        }
        return status;
    }
}
